package com.ayoka.ui.main;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.ayoka.R;
import com.ayoka.base.BaseFragment;
import com.ayoka.base.BasePresenter;
import com.ayoka.databinding.FragmentNavigationBinding;
import com.ayoka.widget.NavigationButton;
import com.jcp.baselibrary.utils.StatusBarUtils;

import java.util.List;


public class NavigationFragment extends BaseFragment<FragmentNavigationBinding, BasePresenter> {

    private NavigationButton mCurrentNavButton;
    private FragmentManager mFragmentManager;
    private int mContainerId;

    @Override
    public int getContentLayout() {
        return R.layout.fragment_navigation;
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void initViews(View view) {
        mDataBinding.navItemKnow.init(R.drawable.tab_icon_home, R.string.main_tab_name_home,
                HomeFragment.class);
        mDataBinding.navItemMeditation.init(R.drawable.tab_icon_meditation, R.string.main_tab_name_meditation,
                MeditationFragment.class);
        mDataBinding.navItemMine.init(R.drawable.tab_icon_mine, R.string.main_tab_name_mine,
                MineFragment.class);
        mDataBinding.navItemKnow.setOnClickListener(this);
        mDataBinding.navItemMeditation.setOnClickListener(this);
        mDataBinding.navItemMine.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    public void setup(FragmentManager fragmentManager, int contentId) {
        mFragmentManager = fragmentManager;
        mContainerId = contentId;
        // do clear
        clearOldFragment();
        // do select first
        doSelect(mDataBinding.navItemKnow);
    }

    @SuppressWarnings("RestrictedApi")
    private void clearOldFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0)
            return;
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != this && fragment != null) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit)
            transaction.commitNow();
    }

    @Override
    public void onClick(View v) {
        if (v instanceof NavigationButton) {
            NavigationButton nav = (NavigationButton) v;
            doSelect(nav);
        }
    }

    private void doSelect(NavigationButton newNavButton) {
        NavigationButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            if (oldNavButton == newNavButton) {
                return;
            }
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton);
        mCurrentNavButton = newNavButton;
    }

    private void doTabChanged(NavigationButton oldNavButton, NavigationButton newNavButton) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.hide(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            Fragment fragment = newNavButton.getFragment();
            if (fragment == null) {
                fragment = Fragment.instantiate(getActivity(),
                        newNavButton.getClx().getName(), null);
                ft.add(mContainerId, fragment, newNavButton.getTag());
                newNavButton.setFragment(fragment);
            } else {
                ft.show(fragment);
            }
            if (fragment instanceof HomeFragment) {
                StatusBarUtils.setStatusTextColor(getActivity(), true);
            } else if (fragment instanceof MeditationFragment) {
                StatusBarUtils.setStatusTextColor(getActivity(), true);
            } else if (fragment instanceof MineFragment) {
                StatusBarUtils.setStatusTextColor(getActivity(), false);
            }
        }
        ft.commitAllowingStateLoss();
    }
}
